package com.example.schoolblog.blog.Service;


import com.example.schoolblog.blog.Mapper.UserMapper;
import com.example.schoolblog.blog.Model.User;
import com.example.schoolblog.blog.Model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    //创建或者更新用户
    public void createOrupdate(User user) {
        //查询用户是否已经在表中
        UserExample userExample = new UserExample();
        userExample.createCriteria().andAccountIdEqualTo(user.getAccountId());
        List<User> Users = userMapper.selectByExample(userExample);
        //判断
        if(Users.size() == 0){
            //插入
            user.setGmtCreate(System.currentTimeMillis());
            user.setGmtModified(user.getGmtCreate());
            userMapper.insert(user);
        }else{
            //更新
            User dbUser = Users.get(0);//获取用户
            dbUser.setGmtModified(System.currentTimeMillis());
            dbUser.setToken(user.getToken());
            UserExample example = new UserExample();
            example.createCriteria().andIdEqualTo(dbUser.getId());
            userMapper.updateByExampleSelective(dbUser,example);
        }
    }
}
